/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ik.servlets;

import com.ik.pbs.Encapsulados.Usuarios;
import com.ik.pbs.Manejadores.ManejadorPerfilesAdministrativos;
import com.ik.pbs.Manejadores.ManejadorUsuarios;
import com.ik.pbs.conection.ConexionDB;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Malkavian
 */
@WebServlet(name = "crearUsuario", urlPatterns = {"/crearUsuario"})
public class crearUsuario extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        ConexionDB con = new ConexionDB();
        try {
            String usuario = request.getParameter("usuario");
            String password = request.getParameter("password");

            Usuarios u = new ManejadorUsuarios().getUsuario(con.getConnectionDB(), usuario);
            

            if (u==null) {
                u = new Usuarios();
                
                u.setId(new ManejadorUsuarios().getNext(con.getConnectionDB()));
                u.setNombre(usuario);
                u.setPassword(password);
                u.setEstado(true);
                u.setFechacreacion(sdf.format(new Date()));
                u.setPerfil(new ManejadorPerfilesAdministrativos().getPerfilAdministrativo(con.getConnectionDB(), 2));
                u.setPuntosNegativos(0);
                u.setPuntosPositivos(0);
                
                if(new ManejadorUsuarios().insertarUsuario(con.getConnectionDB(), u)){
                response.sendRedirect("/PBS/comun/registrarUsuario.jsp?error=1");
                }else{
                response.sendRedirect("/PBS/comun/registrarUsuario.jsp?error=3");
                }
                
            } else {
                response.sendRedirect("/PBS/comun/registrarUsuario.jsp?error=2");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            response.sendRedirect("/PBS/comun/registrarUsuario.jsp?error=3");
        } finally {
            try {
                con.getConnectionDB().close();
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
